package com.lovo.pis.controller;

import com.google.zxing.WriterException;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.utils.CheckCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/code")
public class CheckCodeController {

    @RequestMapping("/getCheckCode")
    public void getCheckCode(HttpServletResponse response, @RequestBody List<Integer> ints) throws IOException {
        //告诉浏览器当前服务器发送的数据格式
        response.setContentType("image/jpeg");
        //生成验证码图片
        BufferedImage checkCode = CheckCodeUtil.getCheckCodeImage(ints);
        //把图片输出到浏览器
        ImageIO.write(checkCode,"jpg",response.getOutputStream());
    }

    @RequestMapping("/getQRCode")
    public void getQRCode(HttpServletResponse response, @RequestBody UserBean userBean) throws IOException, WriterException {
        //告诉浏览器当前服务器发送的数据格式
        response.setContentType("image/jpeg");
        //生成验证码图片
        BufferedImage QRCode = CheckCodeUtil.getQRcode(userBean);
        //把图片输出到浏览器
        ImageIO.write(QRCode,"jpg",response.getOutputStream());
    }

}

